package com.cloudcc.cloudframe.ui.viewgroup.loadmore;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudcc.mobile.R;

public class LoadMoreDefaultFooterView extends RelativeLayout implements
		LoadMoreUIHandler {

	private TextView mTextView;
	private ImageView imageView;
	private LinearLayout llzwjl;
	private Animation operatingAnim;//旋转动画

	public LoadMoreDefaultFooterView(Context context) {
		this(context, null);
	}

	public LoadMoreDefaultFooterView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public LoadMoreDefaultFooterView(Context context, AttributeSet attrs,
									 int defStyle) {
		super(context, attrs, defStyle);
		setupViews();
	}

	private void setupViews() {
		LayoutInflater.from(getContext()).inflate(
				R.layout.cube_views_load_more_default_footer, this);
		mTextView = (TextView) findViewById(R.id.cube_views_load_more_default_footer_text_view);
		imageView = (ImageView) findViewById(R.id.cube_views_load_more_default_footer_imageview);
		llzwjl=(LinearLayout)findViewById(R.id.dynamic_zwjl);
		operatingAnim = AnimationUtils.loadAnimation(getContext(), R.anim.refresh_rotate_anim);
		LinearInterpolator lin = new LinearInterpolator();
		operatingAnim.setInterpolator(lin);
	}

	@Override
	public void onLoading(LoadMoreContainer container) {
		setVisibility(VISIBLE);
		imageView.setVisibility(VISIBLE);
		mTextView.setText(R.string.cube_views_load_more_loading);
		if(operatingAnim != null && !operatingAnim.hasStarted()){
			imageView.startAnimation(operatingAnim);
		}
	}

	@Override
	public void onLoadFinish(LoadMoreContainer container, boolean empty,
							 boolean hasMore) {
		if(operatingAnim != null && operatingAnim.hasStarted()&& imageView != null){
			imageView.clearAnimation();
		}
		imageView.setVisibility(INVISIBLE);
		if (!hasMore) {
			setVisibility(VISIBLE);
			if (empty) {
//				llzwjl.setVisibility(VISIBLE);
//				mTextView.setText(R.string.cube_views_load_more_loaded_empty);
				mTextView.setVisibility(INVISIBLE);
			} else {
				mTextView.setVisibility(VISIBLE);
				llzwjl.setVisibility(GONE);
				mTextView.setText(R.string.cube_views_load_more_loaded_no_more);
			}
		} else {
			setVisibility(INVISIBLE);
		}
	}

	@Override
	public void onWaitToLoadMore(LoadMoreContainer container) {
		setVisibility(VISIBLE);
		if(operatingAnim != null && operatingAnim.hasStarted() && imageView != null){
			imageView.clearAnimation();
		}
		imageView.setVisibility(INVISIBLE);
		mTextView.setText(R.string.cube_views_load_more_click_to_load_more);
	}

	@Override
	public void onLoadError(LoadMoreContainer container, int errorCode,
							String errorMessage) {
		imageView.setVisibility(VISIBLE);
		if(operatingAnim != null && !operatingAnim.hasStarted()){
			imageView.startAnimation(operatingAnim);
		}
		mTextView.setText(R.string.zhengzaijiazais);
	}
}
